package com.ruben.simplescaffold.exception;

import com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta;
import com.ruben.simplescaffold.utils.FunctionUtils;
import com.ruben.simplescaffold.utils.ISupplier;
import org.apache.ibatis.reflection.property.PropertyNamer;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 参数错误异常
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/6/23 11:04
 */
public class ParamException extends BaseException {
    private static final long serialVersionUID = 6464942413911203139L;

    public ParamException(String msg) {
        super(msg);
    }

    /**
     * 类型枚举异常
     *
     * @param typeSupplier 可序列化的Supplier
     * @return com.ruben.xchat.exception.ParamException
     * @author <achao1441470436@gmail.com>
     * @since 2021/10/6 16:08
     */
    public static <T extends Enum<T>> ParamException type(ISupplier<Enum<T>> typeSupplier) {
        LambdaMeta lambda = FunctionUtils.extract(typeSupplier);
        @SuppressWarnings("unchecked")
        Class<Enum<T>> enumClass = (Class<Enum<T>>) lambda.getInstantiatedClass();
        return new ParamException(PropertyNamer.methodToProperty(lambda.getImplMethodName()) + "请传入：[" + Arrays.stream(enumClass.getEnumConstants()).map(Enum::name).collect(Collectors.joining("|")) + "]中的任意一个");
    }
}
